package org.tuoren.official.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.tuoren.common.core.utils.MapstructUtils;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.official.domain.TChannelImages;
import org.tuoren.official.domain.bo.TChannelImagesBo;
import org.tuoren.official.domain.vo.TChannelImagesVo;
import org.tuoren.official.mapper.TChannelImagesMapper;
import org.tuoren.official.service.ITChannelImagesService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 创新联合体轮播图Service业务层处理
 *
 * @author Lion Li
 * @date 2025-03-27
 */
@RequiredArgsConstructor
@Service
public class TChannelImagesServiceImpl implements ITChannelImagesService {

    private final TChannelImagesMapper baseMapper;

    /**
     * 查询创新联合体轮播图
     *
     * @param id 主键
     * @return 创新联合体轮播图
     */
    @Override
    public TChannelImagesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询创新联合体轮播图列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 创新联合体轮播图分页列表
     */
    @Override
    public TableDataInfo<TChannelImagesVo> queryPageList(TChannelImagesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TChannelImages> lqw = buildQueryWrapper(bo);
        Page<TChannelImagesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的创新联合体轮播图列表
     *
     * @param bo 查询条件
     * @return 创新联合体轮播图列表
     */
    @Override
    public List<TChannelImagesVo> queryList(TChannelImagesBo bo) {
        LambdaQueryWrapper<TChannelImages> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TChannelImages> buildQueryWrapper(TChannelImagesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TChannelImages> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getChannelId() != null, TChannelImages::getChannelId, bo.getChannelId());
        lqw.eq(StringUtils.isNotBlank(bo.getImgUrl()), TChannelImages::getImgUrl, bo.getImgUrl());
        lqw.eq(bo.getSort() != null, TChannelImages::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getIsShow()), TChannelImages::getIsShow, bo.getIsShow());
        lqw.orderByAsc(TChannelImages::getSort);
        return lqw;
    }

    /**
     * 新增创新联合体轮播图
     *
     * @param bo 创新联合体轮播图
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TChannelImagesBo bo) {
        TChannelImages add = MapstructUtils.convert(bo, TChannelImages.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改创新联合体轮播图
     *
     * @param bo 创新联合体轮播图
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TChannelImagesBo bo) {
        TChannelImages update = MapstructUtils.convert(bo, TChannelImages.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TChannelImages entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除创新联合体轮播图信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<TChannelImagesVo>  getImages(){
        return baseMapper.selectVoList(new LambdaQueryWrapper<TChannelImages>().eq(TChannelImages::getIsShow,"1").orderByAsc(TChannelImages::getSort));
    }
}
